surface.CreateFont( "coolvetica", 32, 500, true, false, "ScoreboardHeader" )
surface.CreateFont( "coolvetica", 22, 500, true, false, "ScoreboardSubtitle" )
local texGradient 	= surface.GetTextureID( "gui/center_gradient" )
local texLogo 		= surface.GetTextureID( "gui/gmod_logo" )
local PANEL = {}

function PANEL:Init()
	SCOREBOARD = self
	self.Hostname = vgui.Create( "Label", self )
	self.Hostname:SetText( GetGlobalString( "ServerName" ) )
	self.Description = vgui.Create( "Label", self )
	self.Description:SetText( GAMEMODE.Name .. " - " .. GAMEMODE.Author )
	self.Teams = {}
	self.PlayerFrame = vgui.Create( "PlayerFrame", self )
	self.PlayerRows = {}
	self:UpdateScoreboard()
	timer.Create( "ScoreboardUpdater", 1, 0, self.UpdateScoreboard, self )
	self.lblPing = vgui.Create( "Label", self )
	self.lblPing:SetText( "Ping" )
end

function PANEL:AddPlayerRow( ply )
	local button = vgui.Create( "ScorePlayerRow", self.PlayerFrame:GetCanvas() )
	button:SetPlayer( ply )
	self.PlayerRows[ ply ] = button
end

function PANEL:GetPlayerRow( ply )
	return self.PlayerRows[ ply ]
end

function PANEL:Paint()
	draw.RoundedBox( 4, 0, 0, self:GetWide(), self:GetTall(), Color( 10, 10, 10, 255 ) )
	surface.SetTexture( texGradient )
	surface.SetDrawColor( 160, 160, 160, 50 )
	surface.DrawTexturedRect( 0, 0, self:GetWide(), self:GetTall() ) 
	draw.RoundedBox( 4, 4, self.Description.y - 4, self:GetWide() - 8, self:GetTall() - self.Description.y - 4, Color( 30, 30, 30, 255 ) )
	surface.SetTexture( texGradient )
	surface.SetDrawColor( 160, 160, 160, 50 )
	surface.DrawTexturedRect( 4, self.Description.y - 4, self:GetWide() - 8, self:GetTall() - self.Description.y + 30 )
	surface.SetTexture( texGradient )
	surface.SetDrawColor( 160, 160, 160, 50 )
	surface.DrawTexturedRect( 4, self.Description.y - 4, self:GetWide() - 8, self.Description:GetTall() + 30 )
	surface.SetTexture( texLogo )
	surface.SetDrawColor( 255, 255, 255, 255 )
	surface.DrawTexturedRect( 0, 0, 128, 128 ) 
end


/*---------------------------------------------------------
   Name: PerformLayout
---------------------------------------------------------*/
function PANEL:PerformLayout()
	self.Hostname:SizeToContents()
	self.Hostname:SetPos( 115, 16 )
	self.Description:SizeToContents()
	self.Description:SetPos( 128, 64 )
	local iTall = self.PlayerFrame:GetCanvas():GetTall() + self.Description.y + self.Description:GetTall() + 30
	iTall = math.Clamp( iTall, 100, ScrH() * 0.9 )
	local iWide = math.Clamp( ScrW() * 0.8, 700, ScrW() * 0.6 )
	self:SetSize( iWide, iTall + 20 )
	self:SetPos( (ScrW() - self:GetWide()) / 2, (ScrH() - self:GetTall()) / 4 )
	self.PlayerFrame:SetPos( 5, self.Description.y + self.Description:GetTall() + 40 )
	self.PlayerFrame:SetSize( self:GetWide() - 10, self:GetTall() - self.PlayerFrame.y - 10 )
	local y = 0
	local PlayerSorted = {}
	for k, v in pairs( self.PlayerRows ) do
		table.insert( PlayerSorted, v )
	end
	table.sort( PlayerSorted, function ( a , b) return a:HigherOrLower( b ) end )
	for k, v in ipairs( PlayerSorted ) do
		v:SetPos( 0, y )	
		v:SetSize( self.PlayerFrame:GetWide(), v:GetTall() )
		self.PlayerFrame:GetCanvas():SetSize( self.PlayerFrame:GetCanvas():GetWide(), y + v:GetTall() )
		y = y + v:GetTall() + 1
	end
	self.Hostname:SetText( GetGlobalString( "ServerName" ) )
	self.lblPing:SetPos( self:GetWide() - 50, self.PlayerFrame.y - self.lblPing:GetTall() - 3  )
	
end

function PANEL:ApplySchemeSettings()
	self.Hostname:SetFont( "ScoreboardHeader" )
	self.Description:SetFont( "ScoreboardSubtitle" )
	self.Hostname:SetFGColor( Color( 150, 20, 20, 130 ) )
	self.Description:SetFGColor( color_white )
	self.lblPing:SetFont( "ScoreboardText" )
	self.lblPing:SetFGColor( Color( 200, 000, 100, 200 ) )
end


function PANEL:UpdateScoreboard( force )
	if ( !force && !self:IsVisible() ) then return end
	for k, v in pairs( self.PlayerRows ) do
		if ( !k:IsValid() ) then
			v:Remove()
			self.PlayerRows[ k ] = nil
		end
	end
	local PlayerList = player.GetAll()	
	for id, pl in pairs( PlayerList ) do
		if ( !self:GetPlayerRow( pl ) ) then
			self:AddPlayerRow( pl )
		end
	end
	self:InvalidateLayout()
end

vgui.Register( "ScoreBoard", PANEL, "Panel" )